package com.idc.resource.manager;

import com.idc.resource.dal.dto.TUserRoleRelation;
import com.idc.resource.dal.mapper.TRoleMenuRelationMapper;
import com.idc.resource.dal.mapper.TUserRoleRelationMapper;
import com.idc.resource.enums.ErrorCodeEnum;
import com.idc.resource.exception.IdcServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 关系管理
 * Created by Lijing on 2017/7/4.
 */
@Slf4j
@Component
public class RelationManager {
    /** 角色菜单关系 */
    @Autowired
    private TRoleMenuRelationMapper tRoleMenuRelationMapper;
    /** 用户角色关系 */
    @Autowired
    private TUserRoleRelationMapper tUserRoleRelationMapper;

    /**
     * 根据用户id 和角色id 删除用户角色关系
     * @param userId 用户id
     * @param roleId 角色id
     */
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteUserRole(Long userId,Long roleId){
        TUserRoleRelation relation = tUserRoleRelationMapper.selectByUserAndRole(userId,roleId);
        if(relation==null){
            throw new IdcServiceException(ErrorCodeEnum.DATA_ERROR.getCode(),"该用户角色关系不存在");
        }
        int i ;
        try {
            i = tUserRoleRelationMapper.deleteByPrimaryKey(relation.getId());
        }catch (Exception e){
            log.error("根据用户id和角色id删除用户角色关系失败，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
        if(i!=1){
            throw new IdcServiceException(ErrorCodeEnum.DATA_ERROR.getCode(),ErrorCodeEnum.DATA_ERROR.getDesc());
        }
    }

    /**
     * 根据用户id 和角色id 删除用户角色关系
     * @param userId 用户id
     * @param roleId 角色id
     */
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addUserRole(Long userId,Long roleId){
        TUserRoleRelation relation = tUserRoleRelationMapper.selectByUserAndRole(userId,roleId);
        if(relation!=null){
            throw new IdcServiceException(ErrorCodeEnum.DATA_ERROR.getCode(),"该用户角色关系已存在");
        }
        TUserRoleRelation insert = new TUserRoleRelation();
        insert.setRoleId(roleId);
        insert.setUserId(userId);
        insert.setCreatedBy("system");
        int i ;
        try {
            i = tUserRoleRelationMapper.insert(insert);
        }catch (Exception e){
            log.error("新增用户角色关系失败，数据库操作异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
        if(i!=1){
            throw new IdcServiceException(ErrorCodeEnum.DATA_ERROR.getCode(),ErrorCodeEnum.DATA_ERROR.getDesc());
        }
    }

    /**
     * 根据用户id 和角色id 更新用户角色关系
     * @param relation 用户角色关系
     */
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateById(TUserRoleRelation relation){
        TUserRoleRelation selectRelation = tUserRoleRelationMapper.selectByUserAndRole(relation.getUserId(),relation.getRoleId());
        if(selectRelation!=null){
            throw new IdcServiceException(ErrorCodeEnum.DATA_ERROR.getCode(),"该用户角色关系已存在");
        }
        int i ;
        try {
            i= tUserRoleRelationMapper.updateByPrimaryKey(relation);
        }catch (Exception e){
            log.error("更新用户角色关系，数据库操作异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
        if(i!=1){
            throw new IdcServiceException(ErrorCodeEnum.DATA_ERROR.getCode(),ErrorCodeEnum.DATA_ERROR.getDesc());
        }
    }

    /**
     * 根据角色id删除角色菜单关系
     * @param roleId 角色id
     */
    public void deleteRoleMenu(Long roleId){
//        todo 根据角色id删除角色菜单关系
    }




}
